package com.example.ui;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.Communication;
import com.example.util.CallBack;
import com.example.util.Util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FindPassword2Activity extends Activity {
	private Button la_return, la_next;
	private TextView la_title;
	private TextView myMobile;
	private EditText resetPwdEdit;
	private Button resetLoginBtn;
	public static Handler handler;
	public static final int FAIL = 0;
	public static final int SUCCESS = 1;
	public static final int NONE = 2;
	private Intent myIntent;
	private String myTelStr;
	private String codeStr = "";
	private String tel1, tel2;
	private SharedPreferences sp;
	private Editor spEditor;

	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reset_password);
		myIntent = getIntent();
		myTelStr = myIntent.getStringExtra("mobile");
		tel1 = myTelStr.substring(0, 6).toString();
		tel2 = myTelStr.substring(10, 11).toString();
		findView();
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch (msg.what) {
				case FAIL:
					Util.toast(FindPassword2Activity.this, "请检查您的网络");
					break;
				case SUCCESS:
					PushManager.startWork(FindPassword2Activity.this,
							PushConstants.LOGIN_TYPE_API_KEY,
							HSApplication.API_KEY);// 无baidu帐号登录 Leone add
					Communication.login(myTelStr, codeStr,new CallBack() {
						
						@Override
						public void run(Boolean isError, Object data) {
							if(isError){
								if("true".equals(data)){
									 new AlertDialog.Builder(FindPassword2Activity.this).setTitle("提示！登陆成功,请及时修改您的密码")
					        		 .setPositiveButton("确定",
											new DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog,
												int which) {
											Intent i = new Intent();
											i.setClass(FindPassword2Activity.this, MainActivity.class);
											startActivity(i);
											IndexActivity.isLogin = true;
											spEditor.putBoolean("isAutoLogin", true);
											spEditor.commit();
											finish();
										}
									}).show();
									
									return;
								}else if("fasle".equals(data)){
									 new AlertDialog.Builder(FindPassword2Activity.this).setTitle("提示！您的账号已过期请重新续费")
									  .setMessage("请联系客服！")
					        		 .setPositiveButton("确定",
											new DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog,
												int which) {
										}
									}).show();
									return;
								}
							}else{
								if("faile".equals(data)){
									 new AlertDialog.Builder(FindPassword2Activity.this).setTitle("提示登陆失败")
									  .setMessage("账号或者密码错误")
					        		 .setPositiveButton("确定",
											new DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog,
												int which) {
										}
									}).show();
									return;
								}else if("errorResponse".equals(data)){
									new AlertDialog.Builder(FindPassword2Activity.this).setTitle("提示网络异常")
									  .setMessage("请检查您的网络！")
					        		 .setPositiveButton("确定",
											new DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog,
												int which) {
										}
									}).show();
									return;
								}else if("json".equals(data)){
									new AlertDialog.Builder(FindPassword2Activity.this).setTitle("提示数据异常")
									  .setMessage("请联系客服！")
					        		 .setPositiveButton("确定",
											new DialogInterface.OnClickListener() {
										@Override
										public void onClick(DialogInterface dialog,
												int which) {
										}
									}).show();
									return;
								}
							}
						}
					});
					finish();
					break;
				case NONE:
					Util.toast(FindPassword2Activity.this, "密码输入错误");
					break;

				default:
					break;
				}
				super.handleMessage(msg);
			}
		};
	}

	private void findView() {
		la_return = (Button) findViewById(R.id.register_return);
		la_next = (Button) findViewById(R.id.register_next);
		la_title = (TextView) findViewById(R.id.register_title);
		myMobile = (TextView) findViewById(R.id.reset_pwd_tel);
		resetPwdEdit = (EditText) findViewById(R.id.reset_pwd_code_edit);
		resetLoginBtn = (Button) findViewById(R.id.reset_pwd_login);
		init();

	}

	private void init() {
		la_return.setText("返回");
		la_next.setVisibility(View.GONE);
		la_title.setText("填写重置密码");

		sp=HSApplication.mInstance.getSharedPreferences(HSApplication.SPNAME,HSApplication.MODE_PRIVATE);
		spEditor=sp.edit();
		
		myMobile.setText("您的手机号：" + tel1 + "****" + tel2);
		la_return.setOnClickListener(la_ocl);
		resetLoginBtn.setOnClickListener(la_ocl);
	}

	private OnClickListener la_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.register_return:
				finish();
				break;
			case R.id.reset_pwd_login:
				codeStr = resetPwdEdit.getText().toString();
				if ("".equals(codeStr)) {
					Util.toast(FindPassword2Activity.this, "密码输入错误");
				} else {
					Communication.checkNewPassword(myTelStr, codeStr);

				}
				break;

			default:
				break;
			}

		}

	};

}
